<?php

namespace app\devices\controller;

use think\admin\Controller;
use think\admin\extend\CodeExtend;

/**
 * 设备计费管理
 * Class Charge
 * @package app\data\controller
 */
class Charge extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'JcgDeviceFee';

    protected $box_types = [1=>'大柜', 2=>'中柜', 3=>'小柜'];
    protected $fee_types = [3=>'按时', 2=>'按次', 1=>'免费'];

    /**
     * 设备计费管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(){
        $this->title = '设备计费管理';
        $query = $this->_query($this->table);

        $query->setOption("order", ["d_id"=>"ASC"]);

        $query->order('id desc')->equal('status,store_id')->dateBetween('create_at')->page();

    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        $stores = $this->app->db->name("jcg_store")->column("name", 'id');
        $this->assign("stores", $stores);
        if($data){
            foreach ($data as &$row){
                $row['store_name'] = $stores[$row['store_id']]??'';
            }
        }
    }

    /**
     * 添加设备计费
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->title = '添加设备计费';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑设备计费
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->title = '编辑设备计费';
        $this->_form($this->table, 'form');
    }

    /**
     * 查看设备计费
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function see()
    {
        $this->title = '查看设备计费';
        $this->_form($this->table, 'form');
    }

    /**
     * 数据表单处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if($this->app->request->isPost()){
            if($data['fee_type']==2){
                $data['fee_detail'] = json_encode($data['detail'], JSON_UNESCAPED_UNICODE);
            }else{
                $data['fee_detail'] = json_encode($data['detail1'], JSON_UNESCAPED_UNICODE);
            }
            unset($data['detail'], $data['detail1']);
            $query = $this->app->db;

            if(isset($data['id'])){
                try{
                    $query->startTrans();
                    $charge =$query->name($this->table)->where("id", $data['id'])->find();
                    if($charge){
                        $query->name($this->table)->where("id", $data['id'])->update($data);

                        $query->name('jcg_devices')->where("store_id", $charge['store_id'])->update(['fee_type'=>$data['fee_type']]);
                    }else{
                        $this->error("数据有误");
                    }
                }catch (\Exception $e){
                    $this->app->db->rollback();
                    $this->error("修改失败：".$e->getMessage().','.$e->getLine());
                }
                $query->commit();
                $this->success("成功", 'javascript:history.back()');
            }
        }else{
            if($data){
                if($data['fee_type']==2){
                    $data['fee_detail'] = json_decode($data['fee_detail'], true)??[];
                    $data['fee_detail1'] = [];
                }else if($data['fee_type']==3){
                    $data['fee_detail1'] = json_decode($data['fee_detail'], true)??[];
                    $data['fee_detail'] = [];
                }else{
                    $data['fee_detail1'] = $data['fee_detail'] = [];
                }
            }

            $this->assign("stores", $this->app->db->name("jcg_store")->column("name", 'id'));

            $this->assign("use_stores", $this->app->db->name($this->table)->column("store_id"));
        }
    }

    /**
     * 修改设备计费状态
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_save($this->table, $this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除设备计费
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}